package com.hq.controller;

import com.hq.bean.User;
import com.hq.service.MyService;
import com.hq.util.Md5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@Controller
public class MyController {
    @Resource
    private MyService service;

    @RequestMapping("tologin.do")
    public String tologin(){
        return "login";
    }

    @RequestMapping("login.do")
    public String login(User user, HttpSession session) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        user.setPassword(Md5.EncoderByMd5(user.getPassword()));
        User login = service.login(user);
        if (login!=null){
            session.setAttribute("login",login);
            return "redirect:toadd.do";
        }else {
            return "login";
        }
    }

    @RequestMapping("onlyZh.do")
    @ResponseBody
    public Object onlyZh(String zh){
        return service.onlyZh(zh);
    }
}
